#ifndef __SOCKET_TYPE_H__
#define __SOCKET_TYPE_H__

#include "PlatformDiff.h"
//Socket macros
#ifdef WIN32
	#define SHUT_RD                SD_RECEIVE
	#define SHUT_WR                SD_SEND
	#define SHUT_RDWR              SD_BOTH
	#define ACCEPT(a,b,c)          accept(a,b,c)
	#define CONNECT(a,b,c)         connect(a,b,c)
	#define CLOSE(a)               closesocket(a)
	#define READ(a,b,c)            _read(a,b,c)
	#define RECV(a,b,c,d)          recv(a, (char *)b, c, d)
	#define RECVFROM(a,b,c,d,e,f)  recvfrom(a, (char *)b, c, d, (struct sockaddr *)e, (int *)f)
	#define RECV_FLAGS             MSG_WAITALL
	#define SELECT(a,b,c,d,e)      select((int32)a,b,c,d,e)
	#define SEND(a,b,c,d)          send(a, (const char *)b, (int)c, d)
	#define SENDTO(a,b,c,d,e,f)    sendto(a, (const char *)b, (int)c, d, (const struct sockaddr *)(e), f)
	#define SEND_FLAGS             0
	#define SENDFILE(a,b,c,d)      sendfile(a, b, c, d)
	#define SET_SOCKET_ERROR(x,y)  errno=y
	#define SOCKET_ERROR_INTERUPT  EINTR
	#define SOCKET_ERROR_TIMEDOUT  EAGAIN
	#define WRITE(a,b,c)           write(a,b,c)
	#define WRITEV(a,b,c)          Writev(b, c)
	#define GETSOCKOPT(a,b,c,d,e)  getsockopt(a,b,c,(char *)d, (int *)e)
	#define SETSOCKOPT(a,b,c,d,e)  setsockopt(a,b,c,(char *)d, (int)e)
	#define GETHOSTBYNAME(a)       gethostbyname((const char *)a)
	#define LSEEK(a,b,c)           _lseek(a,b,c)
	#define BIND(a,b,c)			   bind(a, (const struct sockaddr*)b,c)
#endif

#if defined(LINUX) || defined(DARWIN)   
	#define ACCEPT(a,b,c)          accept(a,b,c)
	#define CONNECT(a,b,c)         connect(a,b,c)
	#define CLOSE(a)               close(a)
	#define READ(a,b,c)            read(a,b,c)
	#define RECV(a,b,c,d)          recv(a, (void *)b, c, d)
	#define RECVFROM(a,b,c,d,e,f)  recvfrom(a, (char *)b, c, d, (struct sockaddr *)e, f)
	#define RECV_FLAGS             MSG_WAITALL
	#define SELECT(a,b,c,d,e)      select(a,b,c,d,e)
	#define SEND(a,b,c,d)          send(a, (const void *)b, c, d)
	#define SENDTO(a,b,c,d,e,f)    sendto(a, (const void *)b, c, d, (const struct sockaddr *)e, f)
	#define SEND_FLAGS             0
	#define SENDFILE(a,b,c,d)      sendfile(a, b, c, d)
	#define SET_SOCKET_ERROR(x,y)  errno=y
	#define SOCKET_ERROR_INTERUPT  EINTR
	#define SOCKET_ERROR_TIMEDOUT  EAGAIN
	#define WRITE(a,b,c)           write(a,b,c)
	#define WRITEV(a,b,c)          writev(a, b, c)
	#define GETSOCKOPT(a,b,c,d,e)  getsockopt((int)a,(int)b,(int)c,(void *)d,(socklen_t *)e)
	#define SETSOCKOPT(a,b,c,d,e)  setsockopt((int)a,(int)b,(int)c,(const void *)d,(int)e)
	#define GETHOSTBYNAME(a)       gethostbyname((const char *)a)
	#define LSEEK(a,b,c)           lseek(a,b,c)
	#define BIND(a,b,c)			   bind(a, (const struct sockaddr*)b,c)
#endif

#if defined(WIN32)
	typedef int socklen_t;
	struct iovec {
		void  *iov_base;
		size_t iov_len;
	};
#endif

#if defined(LINUX)
	typedef int            SOCKET;
#endif

#ifndef INVALID_SOCKET
	#define INVALID_SOCKET    ~(0)
#endif
#ifndef SOCKET_ERROR
	#define SOCKET_ERROR            (-1)
#endif

// Defines the three possible states for shuting down a socket.
typedef enum 
{
	Receives = SHUT_RD, //Shutdown passive socket.
	Sends = SHUT_WR,    //Shutdown active socket.
	Both = SHUT_RDWR    //Shutdown both active and passive sockets.
} ShutdownMode; 

// Defines the socket types defined by CCSSocket class.
typedef enum  
{
	SocketTypeInvalid,   //Invalid socket type.
	SocketTypeTcp,       //Defines socket as TCP socket.
	SocketTypeUdp,       //Defines socket as UDP socket.
	SocketTypeTcp6,      //Defines socket as IPv6 TCP socket.
	SocketTypeUdp6,      //Defines socket as IPv6 UDP socket.
	SocketTypeRaw        //Provides raw network protocol access.
} SocketType;

// Defines all error codes handled by the CCSSocket class.
typedef enum 
{
	SocketError = -1,          //Generic socket error translates to error below.
	SocketSuccess = 0,         //No socket error.
	SocketInvalidSocket,       //Invalid socket handle.
	SocketInvalidAddress,      //Invalid destination address specified.
	SocketInvalidPort,         //Invalid destination port specified.
	SocketConnectionRefused,   //No server is listening at remote address.
	SocketTimedout,            //Timed out while attempting operation.
	SocketEwouldblock,         //Operation would block if socket were blocking.
	SocketNotconnected,        //Currently not connected.
	SocketEinprogress,         //Socket is non-blocking and the connection cannot be completed immediately
	SocketInterrupted,         //Call was interrupted by a signal that was caught before a valid connection arrived.
	SocketConnectionAborted,   //The connection has been aborted.
	SocketProtocolError,       //Invalid protocol for operation.
	SocketFirewallError,       //Firewall rules forbid connection.
	SocketInvalidSocketBuffer, //The receive buffer point outside the process's address space.
	SocketConnectionReset,     //Connection was forcibly closed by the remote host.
	SocketAddressInUse,        //Address already in use.
	SocketInvalidPointer,      //Pointer type supplied as argument is invalid.
	SocketEunknown             //Unknown error 
} SocketErrorType; 


 

#endif

